package com.springboot.controller;

import com.springboot.annotation.RequireRole;
import com.springboot.common.Result;
import com.springboot.dto.OrderCreateDTO;
import com.springboot.entity.Order;
import com.springboot.service.IOrderService;
import com.springboot.utils.JwtTokenUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zjb
 * @since 2025-10-30
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    /**
     * 创建订单
     *
     * @param orderCreateDTO 订单创建信息
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        // 参数校验
        if (orderCreateDTO == null) {
            return Result.error("请求参数不能为空");
        }
        
        Order order = orderCreateDTO.getOrder();
        if (order == null) {
            return Result.error("订单信息不能为空");
        }
        
        if (orderCreateDTO.getOrderItems() == null) {
            return Result.error("订单明细不能为空");
        }

        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        order.setUserId(userId);
        return orderService.createOrder(order, orderCreateDTO.getOrderItems());
    }

    /**
     * 获取订单列表（分页）
     *
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 订单列表
     */
    @GetMapping("/list")
    public Result getOrders(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        return orderService.getOrdersByUserId(userId, pageNum, pageSize);
    }

    /**
     * 获取订单详情
     *
     * @param orderId 订单号
     * @return 订单详情
     */
    @GetMapping("/{orderId}")
    public Result getOrder(@PathVariable String orderId) {
        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        return orderService.getOrderByOrderId(orderId, userId);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单号
     * @return 取消结果
     */
    @PutMapping("/{orderId}/cancel")
    public Result cancelOrder(@PathVariable String orderId) {
        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        return orderService.cancelOrder(orderId, userId);
    }

    /**
     * 确认收货
     *
     * @param orderId 订单号
     * @return 确认结果
     */
    @PutMapping("/{orderId}/confirm")
    public Result confirmReceipt(@PathVariable String orderId) {
        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        return orderService.confirmReceipt(orderId, userId);
    }

    /**
     * 删除订单（仅限已取消的订单）
     *
     * @param orderId 订单号
     * @return 删除结果
     */
    @DeleteMapping("/{orderId}")
    public Result deleteOrder(@PathVariable String orderId) {
        Integer userId = JwtTokenUtils.getCurrentUser().getUserId();
        return orderService.deleteOrder(orderId, userId);
    }
}